package com.example.actionsignin.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.RepeatAdapter;
import com.example.actionsignin.constant.Constant;
import com.example.actionsignin.mvp.presenter.IReleaseListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/8/5/005.
 */

public class RepeatFragment extends DialogFragment implements
        View.OnClickListener,
        AdapterView.OnItemClickListener {

    private View view;
    private ListView mListView;
    private Button mButton;

    private List<Map<String, Object>> mList = new ArrayList<>();
    private RepeatAdapter mAdapter;

    private IReleaseListener listener = null;

    private int mIndex = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(STYLE_NO_TITLE);

        Window window = this.getDialog().getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.windowAnimations = R.style.BottomDialogAnimation;
        window.setAttributes(layoutParams);

        String key = getArguments().getString("key");
        for (int i = 0; i < Constant.REPEAT_NAME.length; i++) {
            if (key.equals(Constant.REPEAT_NAME[i]))
            {
                mIndex = i;
            }
        }

        if (mIndex == -1)
        {
            mIndex = Constant.REPEAT_NAME.length - 1;
        }

        view = inflater.inflate(R.layout.fragment_dialog_repeat,container,false);
        init();

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        getDialog().getWindow().setLayout((int) (displayMetrics.widthPixels * 0.9f),getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void init() {
        mListView = (ListView) view.findViewById(R.id.list_release_repeat);
        mButton = (Button) view.findViewById(R.id.cancel_repeat);

        setData();
        listener = (IReleaseListener) getActivity();

        mAdapter = new RepeatAdapter(getActivity().getApplicationContext(), mList);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(this);
        mButton.setOnClickListener(this);
    }

    private void setData() {
        for (int i = 0; i < Constant.REPEAT_NAME.length; ++i) {
            Map<String, Object> map = new HashMap<>();

            map.put("type", 0);
            map.put("name", Constant.REPEAT_NAME[i]);
            map.put("flag", false);

            mList.add(map);
        }
        mList.get(mIndex).put("flag",true);
    }

    private void hideData() {
        for (int i = 0; i < Constant.REPEAT_NAME.length; ++i) {
            mList.get(i).put("flag", false);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        hideData();
        mList.get(position).put("flag", true);

        mAdapter.notifyDataSetChanged();
        if (position == Constant.REPEAT_NAME.length-1) {

            RepeatCustomFragment repeatCustomFragment = new RepeatCustomFragment();
            repeatCustomFragment.setCancelable(true);
            repeatCustomFragment.show(getFragmentManager(), "RepeatCustom");
            dismiss();
        } else {
            String data = (String) mList.get(position).get("name");
            listener.sendData(0,data);
            dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id)
        {
            case R.id.cancel_repeat:
                dismiss();
                break;
        }
    }
}
